package com.byron.pss.dao.impl;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.byron.pss.dao.IMessageDAO;
import com.byron.pss.dao.impl.LostFoundDAOImpl.MyResultHandler;
import com.byron.pss.domain.LostFound;
import com.byron.pss.domain.Message;
import com.byron.pss.template.IResultHandler;
import com.byron.pss.template.JdbcTemplate;

public class MessageDAOImpl implements IMessageDAO {

	@Override
	public void save(Message msg) {
		String sql = "INSERT INTO message VALUES(null,?,?,?,?,?)";
		Object[] params = new Object[] { msg.getName(), msg.getEmail(),
				msg.getPhone(), msg.getLostName(),msg.getOther() };
		JdbcTemplate.update(sql, params);
	}

	@Override
	public void delete(Long id) {
		String sql = "DELETE FROM message WHERE id=?";
		JdbcTemplate.update(sql, id);
	}

	@Override
	public List<Message> list() {
		String sql = "SELECT * FROM message";
		List<Message> list = JdbcTemplate.query(sql, new MyResultHandler());
		return list;
	}
	
	class MyResultHandler implements IResultHandler<List<Message>>{

		@Override
		public List<Message> handle(ResultSet rs) throws Exception {
			List<Message> list = new ArrayList<>();
			while(rs.next()){
				long id = rs.getLong("id");
				String name = rs.getString("name");
				String email = rs.getString("email");
				String phone = rs.getString("phone");
				String lostName = rs.getString("lostName");
				String other = rs.getString("other");
				Message msg = new Message(id, name, email, phone, lostName,other);
				list.add(msg);
			}
			return list;
		}
	}
}

